package com.jiantong.admin.controller.dongjiantong;

import com.jiantong.admin.controller.DjtBaseController;
import com.jiantong.admin.dto.*;
import com.jiantong.admin.model.Common;
import com.jiantong.admin.model.PagingMz;
import com.jiantong.user.model.*;
import com.jiantong.user.service.*;
import com.jiantong.work.model.*;
import com.jiantong.work.service.*;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by cao on 2017/7/17.
 * 动物检疫
 */
@Controller
@RequestMapping("/djt/quarantine/")
public class QuarantineController extends DjtBaseController {

    private static final String BaseViews = VIEWS + "quarantine/";

    @RpcConsumer
    private BreedReportsFormReadService breedReportsFormReadService;
    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;
    @RpcConsumer
    private BreedFactoryWriteService breedFactoryWriteService;
    @RpcConsumer
    private BreedReportReadService breedReportReadService;
    @RpcConsumer
    private BreedReportWriteService breedReportWriteService;
    @RpcConsumer
    private BreedReportSurveyRecordReadService breedReportSurveyRecordReadService;
    @RpcConsumer
    private HarmlessReadService harmlessReadService;
    @RpcConsumer
    private HarmlessWriteService harmlessWriteService;
    @RpcConsumer
    private ButcherFactoryReadService butcherFactoryReadService;
    @RpcConsumer
    private ButcherAdmissionInspectionReadService butcherAdmissionInspectionReadService;
    @RpcConsumer
    private ButcherReportReadService butcherReportReadService;
    @RpcConsumer
    private ButcherReportsFormReadService butcherReportsFormReadService;
    @RpcConsumer
    private UserReadService userReadService;
    @RpcConsumer
    private UserProfileReadService userProfileReadService;
    @RpcConsumer
    private GovernmentUserDutyReadService governmentUserDutyReadService;
    @RpcConsumer
    private ProductionReportWriteService productionReportWriteService;
    @RpcConsumer
    private ProductionReportReadService productionReportReadService;

    //动物检疫
    //产地检疫
    //首页
    @GetMapping("placestat")
    private String BreedArchivesDashboard(
            Model model, HttpServletRequest request, HttpServletResponse response
    ) {

        Map mapuser = getarea(response, request);

        model.addAttribute("username", mapuser.get("username"));
        Common common = new Common(breedFactoryReadService, butcherFactoryReadService);
        List areaList = common.serachCityAreaDtoInBreefctory();
        model.addAttribute("areaList", areaList);
        return BaseViews + "placestat";
    }

    @GetMapping("slaughterstat")
    private String slaughterStat(Model model, HttpServletRequest request, HttpServletResponse response) {

        Map mapuser = getarea(response, request);

        model.addAttribute("username", mapuser.get("username"));
        Common common = new Common(breedFactoryReadService, butcherFactoryReadService);
        List areaList = common.findCity_region();
        model.addAttribute("areaList", areaList);
        return BaseViews + "slaughterstat";
    }

    /**
     * 申报地区排行 Top10
     *
     * @return
     */
    @GetMapping("areaorder")
    @ResponseBody
    private List<Map> areaorder() {
        List<Map> mapList = breedReportReadService.areaorder().getResult();
        for (Map map : mapList) {
            BreedFactory factory = breedFactoryReadService.findById((Long) map.get("breedfactoryId")).getResult();
            if (factory == null) {
                map.put("street", "");
            }else{
                map.put("street", factory.getStreet());
            }


        }
        return mapList;
    }

    /**
     * 申报点排行 Top10
     *
     * @return
     */
    @GetMapping("applyorder")
    @ResponseBody
    private List<Map> applyorder() {
        return breedReportReadService.applyorder().getResult();
    }

    /**
     * 产地检疫总体情况
     *
     * @param start
     * @param end
     * @param area
     * @param type
     * @return
     */
    @GetMapping("cdjy")
    @ResponseBody
    private List<Map> cdjy(@RequestParam(required = false) Date start,
                           @RequestParam(required = false) Date end,
                           @RequestParam(required = false) String area,
                           @RequestParam(required = false) Integer type
    ) {
        Map criteria = new HashMap();
        if (area != null && area.length() > 0) {
            criteria.put("address", area);
        }
        if (start != null) {
            criteria.put("start", start);
        }
        if (end != null) {
            criteria.put("end", end);
        }
        if (type != null && type != 0) {
            criteria.put("type", type);
        }
        List<Map> resultList = new ArrayList<>();
        List<Map> mapList = breedReportsFormReadService.cdjy(criteria).getResult();
        Integer number = breedReportsFormReadService.local(criteria).getResult();
        if (number == null) {
            number = 0;
        }
        DecimalFormat df = new DecimalFormat("#0.00");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        if (mapList == null || mapList.size() == 0) {
            Map map1 = new HashMap();
            map1.put("ranking", 0);
            map1.put("num", 0);
            map1.put("qualified", 0);
            map1.put("unqualified", 0);
            map1.put("islocal", 0);
            map1.put("notlocal", 0);
            map1.put("rankingt", 0);
            map1.put("numt", 0);
            map1.put("qualifiedt", 0);
            map1.put("unqualifiedt", 0);
            map1.put("islocalt", 0);
            map1.put("notlocalt", 0);
            map1.put("rankingh", 0);
            map1.put("numh", 0);
            map1.put("qualifiedh", 0);
            map1.put("unqualifiedh", 0);
            map1.put("islocalh", 0);
            map1.put("notlocalh", 0);
            resultList.add(map1);
        } else {
            for (Map map : mapList) {
                if ((Long) map.get("ranking") == 0) {
                    map.put("num", 0);
                    map.put("qualified", 0);
                    map.put("unqualified", 0);
                    map.put("islocal", 0);
                    map.put("notlocal", 0);
                    map.put("rankingt", 0);
                    map.put("numt", 0);
                    map.put("qualifiedt", 0);
                    map.put("unqualifiedt", 0);
                    map.put("islocalt", 0);
                    map.put("notlocalt", 0);
                    map.put("rankingh", 0);
                    map.put("numh", 0);
                    map.put("qualifiedh", 0);
                    map.put("unqualifiedh", 0);
                    map.put("islocalh", 0);
                    map.put("notlocalh", 0);
                } else {
                    map.put("ranking", Double.parseDouble(map.get("ranking").toString()));
                    map.put("islocal", Double.parseDouble(number.toString()));
                    map.put("notlocal", Double.parseDouble(map.get("num").toString()) - number);
                    map.put("num", Double.parseDouble(map.get("num").toString()));
                    map.put("qualified", Double.parseDouble(map.get("qualified").toString()));
                    map.put("unqualified", Double.parseDouble(map.get("unqualified").toString()));
                    //同比
                    c.setTime(start);
                    c.add(Calendar.YEAR, -1);
                    criteria.put("start", c.getTime());
                    c.setTime(end);
                    c.add(Calendar.YEAR, -1);
                    criteria.put("end", c.getTime());
                    List<Map> mapListt = breedReportsFormReadService.cdjy(criteria).getResult();
                    Integer numbert = breedReportsFormReadService.local(criteria).getResult();

                    for (Map mapt : mapListt) {
                        if ((Long) mapt.get("ranking") == 0) {
                            map.put("rankingt", 0);
                            map.put("numt", 0);
                            map.put("qualifiedt", 0);
                            map.put("unqualifiedt", 0);
                            map.put("islocalt", 0);
                            map.put("notlocalt", 0);
                        } else {
                            mapt.put("ranking", Double.parseDouble(mapt.get("ranking").toString()));
                            mapt.put("islocal", Double.parseDouble(numbert.toString()));
                            mapt.put("notlocal", Double.parseDouble(mapt.get("num").toString()) - numbert);
                            mapt.put("num", Double.parseDouble(mapt.get("num").toString()));
                            mapt.put("qualified", Double.parseDouble(mapt.get("qualified").toString()));
                            mapt.put("unqualified", Double.parseDouble(mapt.get("unqualified").toString()));
                            map.put("rankingt", df.format(((Double) map.get("ranking") - (Double) mapt.get("ranking")) / (Double) mapt.get("ranking")));
                            if ((Double) map.get("num") != 0 && (Double) mapt.get("num") != 0) {
                                map.put("numt", df.format(((Double) map.get("num") - (Double) mapt.get("num")) / (Double) mapt.get("num")));
                            } else {
                                map.put("numt", "0");
                            }
                            if ((Double) map.get("qualified") != 0 && (Double) mapt.get("qualified") != 0) {
                                map.put("qualifiedt", df.format(((Double) map.get("qualified") - (Double) mapt.get("qualified")) / (Double) mapt.get("qualified")));
                            } else {
                                map.put("qualifiedt", "0");
                            }
                            if ((Double) map.get("unqualified") != 0 && (Double) mapt.get("unqualified") != 0) {
                                map.put("unqualifiedt", df.format(((Double) map.get("unqualified") - (Double) mapt.get("unqualified")) / (Double) mapt.get("unqualified")));
                            } else {
                                map.put("unqualifiedt", "0");
                            }
                            if ((Double) map.get("islocal") != 0 && (Double) mapt.get("islocal") != 0) {
                                map.put("islocalt", df.format(((Double) map.get("islocal") - (Double) mapt.get("islocal")) / (Double) mapt.get("islocal")));
                            } else {
                                map.put("islocalt", "0");
                            }
                            if ((Double) map.get("notlocal") != 0 && (Double) mapt.get("notlocal") != 0) {
                                map.put("notlocalt", df.format(((Double) map.get("notlocal") - (Double) mapt.get("notlocal")) / (Double) mapt.get("notlocal")));
                            } else {
                                map.put("notlocalt", "0");
                            }
                        }
                    }
                    //环比
                    Long a = end.getTime() - start.getTime();

                    criteria.put("start", new Date(start.getTime() - a));
                    criteria.put("end", start);
                    List<Map> mapListh = breedReportsFormReadService.cdjy(criteria).getResult();
                    Integer numberh = breedReportsFormReadService.local(criteria).getResult();
                    if (numberh == null) {
                        numberh = 0;
                    }
                    for (Map maph : mapListh) {
                        if ((Long) maph.get("ranking") == 0) {
                            map.put("rankingh", 0);
                            map.put("numh", 0);
                            map.put("qualifiedh", 0);
                            map.put("unqualifiedh", 0);
                            map.put("islocalh", 0);
                            map.put("notlocalh", 0);
                        } else {
                            maph.put("ranking", Double.parseDouble(maph.get("ranking").toString()));
                            maph.put("islocal", Double.parseDouble(numberh.toString()));
                            maph.put("notlocal", Double.parseDouble(maph.get("num").toString()) - numberh);
                            maph.put("num", Double.parseDouble(maph.get("num").toString()));
                            maph.put("qualified", Double.parseDouble(maph.get("qualified").toString()));
                            maph.put("unqualified", Double.parseDouble(maph.get("unqualified").toString()));
                            map.put("rankingh", df.format(((Double) map.get("ranking") - (Double) maph.get("ranking")) / (Double) maph.get("ranking")));
                            if ((Double) map.get("num") != 0 && (Double) maph.get("num") != 0) {
                                map.put("numh", df.format(((Double) map.get("num") - (Double) maph.get("num")) / (Double) maph.get("num")));
                            } else {
                                map.put("numh", "0");
                            }
                            if ((Double) map.get("qualified") != 0 && (Double) maph.get("qualified") != 0) {
                                map.put("qualifiedh", df.format(((Double) map.get("qualified") - (Double) maph.get("qualified")) / (Double) maph.get("qualified")));
                            } else {
                                map.put("qualifiedh", "0");
                            }
                            if ((Double) map.get("unqualified") != 0 && (Double) maph.get("unqualified") != 0) {
                                map.put("unqualifiedh", df.format(((Double) map.get("unqualified") - (Double) maph.get("unqualified")) / (Double) maph.get("unqualified")));
                            } else {
                                map.put("unqualifiedh", "0");
                            }
                            if ((Double) map.get("islocal") != 0 && (Double) maph.get("islocal") != 0) {
                                map.put("islocalh", df.format(((Double) map.get("islocal") - (Double) maph.get("islocal")) / (Double) maph.get("islocal")));
                            } else {
                                map.put("islocalh", "0");
                            }
                            if ((Double) map.get("notlocal") != 0 && (Double) maph.get("notlocal") != 0) {
                                map.put("notlocalh", df.format(((Double) map.get("notlocal") - (Double) maph.get("notlocal")) / (Double) maph.get("notlocal")));
                            } else {
                                map.put("notlocalh", "0");
                            }
                        }
                    }


                }
                resultList.add(map);
            }

        }
        return resultList;
    }

    /**
     * 去往本地屠宰场分布图
     *
     * @param start
     * @param end
     * @param area
     * @param type
     * @return
     */
    @GetMapping("localbutcher")
    @ResponseBody
    private List<Map> localbutcher(@RequestParam(required = false) Date start,
                                   @RequestParam(required = false) Date end,
                                   @RequestParam(required = false) String area,
                                   @RequestParam(required = false) Integer type
    ) {
        Map criteria = new HashMap();
        if (area != null && area.length() > 0) {
            criteria.put("address", area);
        }
        if (start != null) {
            criteria.put("start", start);
        }
        if (end != null) {
            criteria.put("end", end);
        }
        if (type != null && type != 0) {
            criteria.put("type", type);
        }
        List<Map> resultList = new ArrayList<>();
        Double allnum = 0d;
        List<Map> mapList = breedReportsFormReadService.localbutcher(criteria).getResult();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DecimalFormat df = new DecimalFormat("#0.00");
        if (mapList != null && mapList.size() > 0) {
            for (Map map : mapList) {
                if ((Long) map.get("ranking") != 0) {
                    map.put("num", Double.parseDouble(map.get("num").toString()));
                    allnum += Double.parseDouble(map.get("num").toString());
                    Calendar c = Calendar.getInstance();
                    c.setTime(start);
                    c.add(Calendar.YEAR, -1);

                    criteria.put("start", c.getTime());
                    c.setTime(end);

                    c.add(Calendar.YEAR, -1);
                    criteria.put("end", c.getTime());
                    List<Map> mapListt = breedReportsFormReadService.localbutcher(criteria).getResult();
                    if (mapListt.size() == 0) {
                        map.put("numt", "0");

                    } else {
                        for (Map mapt : mapListt) {
                            if ((Long) mapt.get("ranking") == 0) {
                                map.put("numt", "0");
                            } else {
                                mapt.put("num", Double.parseDouble(mapt.get("num").toString()));
                                if ((Double) map.get("num") != 0 && (Double) mapt.get("num") != 0) {
                                    map.put("numt", df.format(((Double) map.get("num") - (Double) mapt.get("num")) / (Double) mapt.get("num")));
                                } else {
                                    map.put("numt", "0");
                                }
                            }

                        }
                    }
                    Long a = end.getTime() - start.getTime();
                    Date d = new Date(start.getTime() - a);
                    criteria.put("start", new Date(start.getTime() - a));
                    criteria.put("end", start);
                    List<Map> mapListh = breedReportsFormReadService.localbutcher(criteria).getResult();
                    if (mapListh.size() > 0) {
                        for (Map maph : mapListh) {
                            if ((Long) maph.get("ranking") == 0) {
                                map.put("numh", 0);
                            } else {
                                maph.put("num", Double.parseDouble(maph.get("num").toString()));
                                if ((Double) map.get("num") != 0 && (Double) maph.get("num") != 0) {
                                    map.put("numh", df.format(((Double) map.get("num") - (Double) maph.get("num")) / (Double) maph.get("num")));
                                } else {
                                    map.put("numh", 0);
                                }

                            }
                        }
                    } else {
                        map.put("numh", 0);
                    }
                } else {
                    Map map1 = new HashMap();
                    map1.put("num", 0);
                    map1.put("name", "无数据");
                    map1.put("numt", 0);
                    map1.put("numh", 0);
                    map1.put("allnum", 0);
                    resultList.add(map1);
                }
                map.put("allnum", allnum);
                resultList.add(map);
            }
        } else {
            Map map = new HashMap();
            map.put("num", 0);
            map.put("name", "无数据");
            map.put("numt", 0);
            map.put("numh", 0);
            map.put("allnum", 0);
            resultList.add(map);
        }
        return resultList;
    }

    //屠宰检疫分析
    //屠宰总体情况
    @GetMapping("tzjy")
    @ResponseBody
    private List<Map> tzjy(@RequestParam(required = false) Date start,
                           @RequestParam(required = false) Date end,
                           @RequestParam(required = false) String area,
                           @RequestParam(required = false) Integer type
    ) {
        Map criteria = new HashMap();
        List<ButcherFactory> list = new ArrayList<>();
        if (area != null && area.length() > 0) {
            list = butcherFactoryReadService.findByRegion(area).getResult();
        }
        List<Long> ids = new ArrayList<>();
        if (list != null) {
            for (ButcherFactory butfac : list) {
                ids.add(butfac.getId());
            }
        }

        if (start != null) {
            criteria.put("start", start);
        }
        if (end != null) {
            criteria.put("end", end);
        }
        if (type != null && type != 0) {
            criteria.put("type", type);
        }
        Integer number = butcherReportsFormReadService.local(criteria).getResult();
        if (number == null) {
            number = 0;
        }
        if (ids.size() > 0) {
            criteria.put("ids", ids);
        }
        List<Map> resultList = new ArrayList<>();
        List<Map> mapList = butcherReportsFormReadService.tzjy(criteria).getResult();
        DecimalFormat df = new DecimalFormat("#0.00");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        if (mapList == null || mapList.size() == 0) {
            Map map1 = new HashMap();
            map1.put("ranking", 0);
            map1.put("num", 0);
            map1.put("qualified", 0);
            map1.put("unqualified", 0);
            map1.put("islocal", 0);
            map1.put("notlocal", 0);
            map1.put("rankingt", 0);
            map1.put("numt", 0);
            map1.put("qualifiedt", 0);
            map1.put("unqualifiedt", 0);
            map1.put("islocalt", 0);
            map1.put("notlocalt", 0);
            map1.put("rankingh", 0);
            map1.put("numh", 0);
            map1.put("qualifiedh", 0);
            map1.put("unqualifiedh", 0);
            map1.put("islocalh", 0);
            map1.put("notlocalh", 0);
            resultList.add(map1);

        } else {
            for (Map map : mapList) {
                if ((Long) map.get("ranking") == 0) {
                    map.put("num", 0);
                    map.put("qualified", 0);
                    map.put("unqualified", 0);
                    map.put("islocal", 0);
                    map.put("notlocal", 0);
                } else {
                    map.put("ranking", Double.parseDouble(map.get("ranking").toString()));
                    map.put("islocal", Double.parseDouble(number.toString()));
                    map.put("notlocal", Double.parseDouble(map.get("num").toString()) - number);
                    map.put("num", Double.parseDouble(map.get("num").toString()));
                    map.put("qualified", Double.parseDouble(map.get("qualified").toString()));
                    map.put("unqualified", Double.parseDouble(map.get("unqualified").toString()));
                    //同比
                    c.setTime(start);
                    c.add(Calendar.YEAR, -1);
                    criteria.put("start", c.getTime());
                    c.setTime(end);
                    c.add(Calendar.YEAR, -1);
                    criteria.put("end", c.getTime());
                    List<Map> mapListt = butcherReportsFormReadService.tzjy(criteria).getResult();
                    Integer numbert = butcherReportsFormReadService.local(criteria).getResult();

                    for (Map mapt : mapListt) {
                        if ((Long) mapt.get("ranking") == 0) {
                            map.put("rankingt", 0);
                            map.put("numt", 0);
                            map.put("qualifiedt", 0);
                            map.put("unqualifiedt", 0);
                            map.put("islocalt", 0);
                            map.put("notlocalt", 0);
                        } else {
                            mapt.put("ranking", Double.parseDouble(mapt.get("ranking").toString()));
                            mapt.put("islocal", Double.parseDouble(numbert.toString()));
                            mapt.put("notlocal", Double.parseDouble(mapt.get("num").toString()) - numbert);
                            mapt.put("num", Double.parseDouble(mapt.get("num").toString()));
                            mapt.put("qualified", Double.parseDouble(mapt.get("qualified").toString()));
                            mapt.put("unqualified", Double.parseDouble(mapt.get("unqualified").toString()));
                            map.put("rankingt", df.format(((Double) map.get("ranking") - (Double) mapt.get("ranking")) / (Double) mapt.get("ranking")));
                            if ((Double) map.get("num") != 0 && (Double) mapt.get("num") != 0) {
                                map.put("numt", df.format(((Double) map.get("num") - (Double) mapt.get("num")) / (Double) mapt.get("num")));
                            } else {
                                map.put("numt", "0");
                            }
                            if ((Double) map.get("qualified") != 0 && (Double) mapt.get("qualified") != 0) {
                                map.put("qualifiedt", df.format(((Double) map.get("qualified") - (Double) mapt.get("qualified")) / (Double) mapt.get("qualified")));
                            } else {
                                map.put("qualifiedt", "0");
                            }
                            if ((Double) map.get("unqualified") != 0 && (Double) mapt.get("unqualified") != 0) {
                                map.put("unqualifiedt", df.format(((Double) map.get("unqualified") - (Double) mapt.get("unqualified")) / (Double) mapt.get("unqualified")));
                            } else {
                                map.put("unqualifiedt", "0");
                            }
                            if ((Double) map.get("islocal") != 0 && (Double) mapt.get("islocal") != 0) {
                                map.put("islocalt", df.format(((Double) map.get("islocal") - (Double) mapt.get("islocal")) / (Double) mapt.get("islocal")));
                            } else {
                                map.put("islocalt", "0");
                            }
                            if ((Double) map.get("notlocal") != 0 && (Double) mapt.get("notlocal") != 0) {
                                map.put("notlocalt", df.format(((Double) map.get("notlocal") - (Double) mapt.get("notlocal")) / (Double) mapt.get("notlocal")));
                            } else {
                                map.put("notlocalt", "0");
                            }
                        }
                    }
                    //环比
                    Long a = end.getTime() - start.getTime();

                    criteria.put("start", new Date(start.getTime() - a));
                    criteria.put("end", start);
                    List<Map> mapListh = butcherReportsFormReadService.tzjy(criteria).getResult();
                    Integer numberh = butcherReportsFormReadService.local(criteria).getResult();

                    for (Map maph : mapListh) {
                        if ((Long) maph.get("ranking") == 0) {
                            map.put("rankingh", 0);
                            map.put("numh", 0);
                            map.put("qualifiedh", 0);
                            map.put("unqualifiedh", 0);
                            map.put("islocalh", 0);
                            map.put("notlocalh", 0);
                        } else {
                            maph.put("ranking", Double.parseDouble(maph.get("ranking").toString()));
                            maph.put("islocal", Double.parseDouble(numberh.toString()));
                            maph.put("notlocal", Double.parseDouble(maph.get("num").toString()) - numberh);
                            maph.put("num", Double.parseDouble(maph.get("num").toString()));
                            maph.put("qualified", Double.parseDouble(maph.get("qualified").toString()));
                            maph.put("unqualified", Double.parseDouble(maph.get("unqualified").toString()));
                            map.put("rankingh", df.format(((Double) map.get("ranking") - (Double) maph.get("ranking")) / (Double) maph.get("ranking")));
                            if ((Double) map.get("num") != 0 && (Double) maph.get("num") != 0) {
                                map.put("numh", df.format(((Double) map.get("num") - (Double) maph.get("num")) / (Double) maph.get("num")));
                            } else {
                                map.put("numh", "0");
                            }
                            if ((Double) map.get("qualified") != 0 && (Double) maph.get("qualified") != 0) {
                                map.put("qualifiedh", df.format(((Double) map.get("qualified") - (Double) maph.get("qualified")) / (Double) maph.get("qualified")));
                            } else {
                                map.put("qualifiedh", "0");
                            }
                            if ((Double) map.get("unqualified") != 0 && (Double) maph.get("unqualified") != 0) {
                                map.put("unqualifiedh", df.format(((Double) map.get("unqualified") - (Double) maph.get("unqualified")) / (Double) maph.get("unqualified")));
                            } else {
                                map.put("unqualifiedh", "0");
                            }
                            if ((Double) map.get("islocal") != 0 && (Double) maph.get("islocal") != 0) {
                                map.put("islocalh", df.format(((Double) map.get("islocal") - (Double) maph.get("islocal")) / (Double) maph.get("islocal")));
                            } else {
                                map.put("islocalh", "0");
                            }
                            if ((Double) map.get("notlocal") != 0 && (Double) maph.get("notlocal") != 0) {
                                map.put("notlocalh", df.format(((Double) map.get("notlocal") - (Double) maph.get("notlocal")) / (Double) maph.get("notlocal")));
                            } else {
                                map.put("notlocalh", "0");
                            }
                        }
                    }


                }
                resultList.add(map);
            }
        }
        return resultList;

    }

    //屠宰来源分布图
    @GetMapping("tzly")
    @ResponseBody
    private List<Map> tzly(@RequestParam(required = false) Date start,
                           @RequestParam(required = false) Date end,
                           @RequestParam(required = false) String area,
                           @RequestParam(required = false) Integer type
    ) {
        Map criteria = new HashMap();
        List<ButcherFactory> list = new ArrayList<>();
        if (area != null && area.length() > 0) {
            list = butcherFactoryReadService.findByRegion(area).getResult();
        }
        List<Long> ids = new ArrayList<>();
        if (list != null) {
            for (ButcherFactory butfac : list) {
                ids.add(butfac.getId());
            }
        }
        if (start != null) {
            criteria.put("start", start);
        }
        if (end != null) {
            criteria.put("end", end);
        }
        if (type != null && type != 0) {
            criteria.put("type", type);
        }
        if (ids.size() > 0) {
            criteria.put("ids", ids);
        }
        List<Map> resultList = new ArrayList<>();
        List<Map> mapList = butcherReportsFormReadService.localbutcher(criteria).getResult();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DecimalFormat df = new DecimalFormat("#0.00");
        if (mapList.size() == 0) {
            Map map1 = new HashMap();
            map1.put("num", 0);
            map1.put("name", "无数据");
            map1.put("numt", 0);
            map1.put("numh", 0);
            mapList = new ArrayList<>();
            mapList.add(map1);

        } else {
            for (Map map : mapList) {
                if ((Long) map.get("ranking") == 0) {
                    map.put("num", 0);
                    map.put("name", "无数据");
                    mapList = new ArrayList<>();
                    mapList.add(map);
                } else {
                    map.put("num", Double.parseDouble(map.get("num").toString()));
                    Calendar c = Calendar.getInstance();
                    c.setTime(start);
                    c.add(Calendar.YEAR, -1);

                    criteria.put("start", c.getTime());
                    c.setTime(end);

                    c.add(Calendar.YEAR, -1);
                    criteria.put("end", c.getTime());
                    List<Map> mapListt = butcherReportsFormReadService.localbutcher(criteria).getResult();
                    if (mapListt.size() == 0) map.put("numt", "0");
                    for (Map mapt : mapListt) {
                        if ((Long) mapt.get("ranking") == 0) {
                            map.put("numt", "0");
                        } else {
                            mapt.put("num", Double.parseDouble(mapt.get("num").toString()));
                            if ((Double) map.get("num") != 0 && (Double) mapt.get("num") != 0) {
                                map.put("numt", df.format(((Double) map.get("num") - (Double) mapt.get("num")) / (Double) mapt.get("num")));
                            } else {
                                map.put("numt", "0");
                            }
                        }
                    }
                    Long a = end.getTime() - start.getTime();
                    Date d = new Date(start.getTime() - a);
                    criteria.put("start", new Date(start.getTime() - a));
                    criteria.put("end", start);
                    List<Map> mapListh = butcherReportsFormReadService.localbutcher(criteria).getResult();
                    if (mapListh.size() == 0) map.put("numh", 0);
                    for (Map maph : mapListh) {
                        if ((Long) maph.get("ranking") == 0) {
                            map.put("numh", 0);
                        } else {
                            maph.put("num", Double.parseDouble(maph.get("num").toString()));
                            if ((Double) map.get("num") != 0 && (Double) maph.get("num") != 0) {
                                map.put("numh", df.format(((Double) map.get("num") - (Double) maph.get("num")) / (Double) maph.get("num")));
                            } else {
                                map.put("numh", "0");
                            }

                        }

                    }
                }
                resultList.add(map);
            }
        }
        return mapList;
    }

    //申报数据管理
    @GetMapping("declarmanage")
    private String declarmanage(Model model, HttpServletRequest request, HttpServletResponse response) {
        Map map = getarea(response, request);
        model.addAttribute("areaList", map.get("cityAreaDtoList"));

        model.addAttribute("breedFactoryList", map.get("breedFactorys"));
        model.addAttribute("username", map.get("username"));

        return BaseViews + "declaration-management";
    }

    @RequestMapping("declarIndex")
    @ResponseBody
    private PagingMz<Report_ReportSurveyDto> showDeclarIndex(@RequestParam(required = false) Integer page,
                                                             @RequestParam(required = false) Integer rows,
                                                             @RequestParam(required = false) Date start,
                                                             @RequestParam(required = false) Date end,
                                                             @RequestParam(required = false) String area,
                                                             @RequestParam(required = false) Integer type,
                                                             @RequestParam(required = false) Integer status
    ) {
        Map criteria = new HashMap();
        List<BreedFactory> facList = findFactoryIdByArea(area, -1);
        List<Long> breedFactoryIds = new ArrayList<>();
        for (BreedFactory breedFactory : facList) {
            breedFactoryIds.add(breedFactory.getId());
        }

        criteria.clear();
        if (start != null) {
            criteria.put("fromDate", start);
        }
        if (end != null) {
            criteria.put("toDate", end);
        }
        if (type != null && type != 0) {
            criteria.put("breedType", type);
        }
        if (status != null && status != -5) {
            criteria.put("status", status);
        }
        criteria.put("breedFactoryIds", breedFactoryIds);
        criteria.put("deleteFlag", 1);
        List<Report_ReportSurveyDto> reportList = new ArrayList<>();
        Paging<BreedReport> breedReportPaging = (Paging<BreedReport>) breedReportReadService.pagingCommon(page, rows, criteria).getResult();


        for (BreedReport breedReport : breedReportPaging.getData()) {
            Report_ReportSurveyDto report_reportSurveyDto = new Report_ReportSurveyDto();
            report_reportSurveyDto.setBreedReport(breedReport);
            BreedReportSurveyRecord breedReportSurveyRecord = breedReportSurveyRecordReadService.findByReportId(breedReport.getId()).getResult();
            report_reportSurveyDto.setBreedReportSurveyRecord(breedReportSurveyRecord);
            reportList.add(report_reportSurveyDto);
        }
        Long records = breedReportPaging.getTotal();
        Long pagetotal = (records - 1) / rows + 1;
        return new PagingMz<>(pagetotal, records, page, reportList);
    }

    @GetMapping("managementDetail")
    private String managementDetail() {


        return BaseViews + "managementDetail";
    }


    //检疫不合格处理
    @GetMapping("quarantineun")
    private String quarantineun(Model model, HttpServletRequest request, HttpServletResponse response) {

        Map map = getarea(response, request);
        model.addAttribute("areaList", map.get("cityAreaDtoList"));

        model.addAttribute("breedFactoryList", map.get("breedFactorys"));
        model.addAttribute("username", map.get("username"));

        return BaseViews + "quarantine-unqualified";
    }

    @GetMapping("quarantineIndex")
    @ResponseBody
    private PagingMz<Fac_HarmlessDto> showQuarantineIndex(@RequestParam(required = false) Integer page,
                                                          @RequestParam(required = false) Integer rows,
                                                          @RequestParam(required = false) Date start,
                                                          @RequestParam(required = false) Date end,
                                                          @RequestParam(required = false) String area,
                                                          @RequestParam(required = false) Integer type,
                                                          @RequestParam(required = false) Integer method
    ) {
        Map criteria = new HashMap();
        List<BreedFactory> facList = findFactoryIdByArea(area, -1);
        criteria.clear();
        if (start != null) {
            criteria.put("fromDate", start);
        }
        if (end != null) {
            criteria.put("toDate", end);
        }
        if (type != null && type != 0) {
            criteria.put("breedType", type);
        }
        if (method != null && method != 0) {
            criteria.put("method", method);
        }
        criteria.put("deleteFlag", 1);
        Paging<Harmless> paging = harmlessReadService.pagingCommon(page, rows, criteria).getResult();
        List<Fac_HarmlessDto> fac_harmlessList = new ArrayList<>();
        for (BreedFactory fac : facList) {
            for (Harmless harm : paging.getData()) {
                if (fac.getId().equals(harm.getBreedFactoryId())) {
                    fac_harmlessList.add(new Fac_HarmlessDto(fac, harm));
                }
            }
        }

        Long records = paging.getTotal();
        Long pagetotal = (records - 1) / rows + 1;
        return new PagingMz<>(pagetotal, records, page, fac_harmlessList);
    }

    @GetMapping("unqualified")
    private String unqualified() {
        return BaseViews + "unqualifiedDetail";
    }


    //屠宰检疫
    //入场监督查验
    @GetMapping("admission")
    private String admission(Model model) {
        Common common = new Common(breedFactoryReadService, butcherFactoryReadService);
        List areaList = common.findCity_region();
        model.addAttribute("areaList", areaList);
        return BaseViews + "admission-supervision";
    }

    @GetMapping("admissionindex")
    @ResponseBody
    private PagingMz<Butcher_admissionDto> showAdmissionIndex(@RequestParam(required = false) Integer page,
                                                              @RequestParam(required = false) Integer rows,
                                                              @RequestParam(required = false) Date start,
                                                              @RequestParam(required = false) Date end,
                                                              @RequestParam(required = false) String area,
                                                              @RequestParam(required = false) Integer type,
                                                              @RequestParam(required = false) Integer status
    ) {
        Map criteria = new HashMap();
        if (area != null && area.length() > 0) {
            criteria.put("productName", area);
        }
        List<ButcherFactory> butList = (List<ButcherFactory>) butcherFactoryReadService.findAllButcherFactory().getResult().getData();
        criteria.clear();
        if (start != null) {
            criteria.put("start", start);
        }
        if (end != null) {
            criteria.put("end", end);
        }
        if (type != null && type != 0) {
            criteria.put("productType", type);
        }
        if (status != null && status != -1) {
            criteria.put("status", status);
        }
        Paging<ButcherAdmissionInspection> paging = butcherAdmissionInspectionReadService.pagingcommon(page, rows, criteria).getResult();
        List<Butcher_admissionDto> butadmissionList = new ArrayList<>();
        for (ButcherFactory butcher : butList) {
            for (ButcherAdmissionInspection admission : paging.getData()) {
                if (butcher.getId().equals(admission.getButcherFactoryId())) {
                    butadmissionList.add(new Butcher_admissionDto(butcher, admission));
                }
            }
        }

        Long records = paging.getTotal();
        Long pagetotal = (records - 1) / rows + 1;
        return new PagingMz<>(pagetotal, records, page, butadmissionList);
    }

    @GetMapping("admissionpage")
    private String admissionpage() {
        return BaseViews + "admissionDetail";
    }


    //屠宰检疫申报
    @GetMapping("declaration")
    private String declaration(Model model) {
        Common common = new Common(breedFactoryReadService, butcherFactoryReadService);
        List areaList = common.findCity_region();
        model.addAttribute("areaList", areaList);
        return BaseViews + "quarantine-declaration";
    }

    @GetMapping("declarationindex")
    @ResponseBody
    private PagingMz<Butcher_ReportDto> showDeclarationIndex(@RequestParam(required = false) Integer page,
                                                             @RequestParam(required = false) Integer rows,
                                                             @RequestParam(required = false) Date start,
                                                             @RequestParam(required = false) Date end,
                                                             @RequestParam(required = false) String area,
                                                             @RequestParam(required = false) Integer type,
                                                             @RequestParam(required = false) Integer status
    ) {
        Map criteria = new HashMap();
        if (area != null && area.length() > 0) {
            criteria.put("productName", area);
        }
        List<ButcherFactory> butList = (List<ButcherFactory>) butcherFactoryReadService.findAllButcherFactory().getResult().getData();
        criteria.clear();
        if (start != null) {
            criteria.put("start", start);
        }
        if (end != null) {
            criteria.put("end", end);
        }
        if (type != null && type != 0) {
            criteria.put("productType", type);
        }
        if (status != null && status != -1) {
            criteria.put("status", status);
        }
        Paging<ButcherReport> paging = butcherReportReadService.pagingCommon(page, rows, criteria).getResult();
        List<Butcher_ReportDto> butadReportList = new ArrayList<>();
        for (ButcherFactory butcher : butList) {
            for (ButcherReport butcherReport : paging.getData()) {
                if (butcher.getId().equals(butcherReport.getButcherFactoryId())) {
                    butadReportList.add(new Butcher_ReportDto(butcher, butcherReport));
                }
            }
        }
        Long records = paging.getTotal();
        Long pagetotal = (records - 1) / rows + 1;
        return new PagingMz<>(pagetotal, records, page, butadReportList);
    }

    @GetMapping("declarationpage")
    private String declarationpage() {
        return BaseViews + "declarationDetail";
    }

    //宰前检查
    @GetMapping("preInspection")
    private String preInspection(Model model) {
        Common common = new Common(breedFactoryReadService, butcherFactoryReadService);
        List areaList = common.findCity_region();
        model.addAttribute("areaList", areaList);
        return BaseViews + "pre-Inspection";
    }

    @GetMapping("preInspectionIndex")
    @ResponseBody
    private PagingMz<Butcher_Form_admissionDto> showPreInspectionIndex(@RequestParam(required = false) Integer
                                                                               page,
                                                                       @RequestParam(required = false) Integer rows,
                                                                       @RequestParam(required = false) Date start,
                                                                       @RequestParam(required = false) Date end,
                                                                       @RequestParam(required = false) String area,
                                                                       @RequestParam(required = false) Integer type
    ) {
        Map criteria = new HashMap();
        if (area != null && area.length() > 0) {
            criteria.put("productName", area);
        }
        List<ButcherFactory> butList = (List<ButcherFactory>) butcherFactoryReadService.findAllButcherFactory().getResult().getData();
        criteria.clear();
        if (start != null) {
            criteria.put("start", start);
        }
        if (end != null) {
            criteria.put("end", end);
        }
        if (type != null && type != 0) {
            criteria.put("productType", type);
        }
        Paging<ButcherReportsForm> paging = butcherReportsFormReadService.pagingCommon(page, rows, criteria).getResult();
        List<Butcher_Form_admissionDto> butadReportFormList = new ArrayList<>();
        for (ButcherFactory butcher : butList) {
            for (ButcherReportsForm butcherReportForm : paging.getData()) {
                if (butcher.getId().equals(butcherReportForm.getButcherFactoryId())) {
                    Map map = new HashMap();
                    map.put("butcherFactoryId", butcher.getId());
                    ButcherAdmissionInspection butcherAdmissionInspection = butcherAdmissionInspectionReadService.pagingcommon(0, 1, map).getResult().getData().get(0);
                    butadReportFormList.add(new Butcher_Form_admissionDto(butcher, butcherAdmissionInspection, butcherReportForm));
                }
            }
        }
        Long records = paging.getTotal();
        Long pagetotal = (records - 1) / rows + 1;
        return new PagingMz<>(pagetotal, records, page, butadReportFormList);
    }

    @GetMapping("preInspectionpage")
    private String preInspectionpage() {
        return BaseViews + "preDetail";
    }


    //同步检疫
    @GetMapping("simultaneous")
    private String simultaneous(Model model) {
        Common common = new Common(breedFactoryReadService, butcherFactoryReadService);
        List areaList = common.findCity_region();
        model.addAttribute("areaList", areaList);
        return BaseViews + "simultaneous-quarantine";
    }

    @GetMapping("simultaneousIndex")
    @ResponseBody
    private PagingMz<Butcher_Form_admissionDto> showSimultaneousIndex(@RequestParam(required = false) Integer
                                                                              page,
                                                                      @RequestParam(required = false) Integer rows,
                                                                      @RequestParam(required = false) Date start,
                                                                      @RequestParam(required = false) Date end,
                                                                      @RequestParam(required = false) String area,
                                                                      @RequestParam(required = false) Integer type
    ) {
        Map criteria = new HashMap();
        if (area != null && area.length() > 0) {
            criteria.put("productName", area);
        }
        List<ButcherFactory> butList = (List<ButcherFactory>) butcherFactoryReadService.findAllButcherFactory().getResult().getData();
        criteria.clear();
        if (start != null) {
            criteria.put("start", start);
        }
        if (end != null) {
            criteria.put("end", end);
        }
        if (type != null && type != 0) {
            criteria.put("productType", type);
        }
        Paging<ButcherReportsForm> paging = butcherReportsFormReadService.pagingCommon(page, rows, criteria).getResult();
        List<Butcher_Form_admissionDto> butadReportFormList = new ArrayList<>();
        for (ButcherFactory butcher : butList) {
            for (ButcherReportsForm butcherReportForm : paging.getData()) {
                if (butcher.getId().equals(butcherReportForm.getButcherFactoryId())) {
                    Map map = new HashMap();
                    map.put("butcherFactoryId", butcher.getId());
                    ButcherAdmissionInspection butcherAdmissionInspection = butcherAdmissionInspectionReadService.pagingcommon(0, 1, map).getResult().getData().get(0);
                    butadReportFormList.add(new Butcher_Form_admissionDto(butcher, butcherAdmissionInspection, butcherReportForm));
                }
            }
        }
        Long records = paging.getTotal();
        Long pagetotal = (records - 1) / rows + 1;
        return new PagingMz<>(pagetotal, records, page, butadReportFormList);
    }

    @GetMapping("simultaneouspage")
    private String simultaneouspage() {
        return BaseViews + "simultaneousDetail";
    }

    //合格发证
    @GetMapping("certification")
    private String certification(Model model) {
        Common common = new Common(breedFactoryReadService, butcherFactoryReadService);
        List areaList = common.findCity_region();
        model.addAttribute("areaList", areaList);
        return BaseViews + "qualified-certification";
    }

    @GetMapping("certificationIndex")
    @ResponseBody
    private PagingMz<Butcher_Report_formDto> showCertificationIndex(@RequestParam(required = false) Integer
                                                                            page,
                                                                    @RequestParam(required = false) Integer rows,
                                                                    @RequestParam(required = false) Date start,
                                                                    @RequestParam(required = false) Date end,
                                                                    @RequestParam(required = false) String area,
                                                                    @RequestParam(required = false) Integer type,
                                                                    @RequestParam(required = false) Integer status

    ) {
        Map criteria = new HashMap();
        if (area != null && area.length() > 0) {
            criteria.put("productName", area);
        }
        List<ButcherFactory> butList = (List<ButcherFactory>) butcherFactoryReadService.findAllButcherFactory().getResult().getData();
        criteria.clear();
        if (start != null) {
            criteria.put("start", start);
        }
        if (end != null) {
            criteria.put("end", end);
        }
        if (type != null && type != 0) {
            criteria.put("productType", type);
        }
        if (status != null && status != -5) {
            criteria.put("status", status);
        }
        Paging<ButcherReport> paging = butcherReportReadService.pagingCommon(page, rows, criteria).getResult();
        List<Butcher_Report_formDto> brfList = new ArrayList<>();
        for (ButcherFactory butcherFactory : butList) {
            for (ButcherReport butcherReport : paging.getData()) {
                if (butcherFactory.getId().equals(butcherReport.getButcherFactoryId())) {
                    Map map = new HashMap();
                    map.put("reportNumber", butcherReport.getNumber());
                    List<ButcherReportsForm> butcherReportsFormList = butcherReportsFormReadService.pagingCommon(0, 1000, map).getResult().getData();
                    if (butcherReportsFormList.size() > 0) {
                        ButcherReportsForm butcherReportsForm = butcherReportsFormList.get(0);
                        brfList.add(new Butcher_Report_formDto(butcherReport, butcherReportsForm, butcherFactory));
                    }
                }
            }
        }
        Long records = paging.getTotal();
        Long pagetotal = (records - 1) / rows + 1;
        return new PagingMz<>(pagetotal, records, page, brfList);

    }

    @GetMapping("certificationpage")
    private String certificationpage() {
        return BaseViews + "certificationDetail";
    }


    public Map getarea(HttpServletResponse response, HttpServletRequest request) {
        List<String> list = new ArrayList<>();
        list.add("province");
        list.add("city");
        list.add("region");
        list.add("street");
        list.add("village");
        Cookie[] cookies = null;
        cookies = request.getCookies();
        String name = "";
        Map map = new HashMap();
        for (Cookie cookie : cookies) {

            if (cookie.getName().equals("djtid")) {
                String id = cookie.getValue();
                int Id = Integer.parseInt(id);
                Response<User> userResponse = userReadService.findById(Integer.toUnsignedLong(Id));
                name = userResponse.getResult().getName();
                map.put("username", name);
                switch (userResponse.getResult().getAreaLevel()) {
                    case 1:
                        UserProfile userProfile = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String Province = userProfile.getProvince();
                        Map map6 = new HashMap();
                        map6.put("province", Province);
                        List<BreedFactory> breedFactoryList = (List<BreedFactory>) breedFactoryReadService.findByArea(map6).getResult();
                        //获取所有省下城市

                        Map map1 = new HashMap();
                        map1.put("area1", list.get(1));
                        map1.put("area2", list.get(0));
                        map1.put("area3", Province);
                        List<String> citys = breedFactoryReadService.serachAllregion1(map1);
                        //获取城市下所有区
                        List<CityAreaDto> cityAreaDtoList = new ArrayList<>();
                        for (String city : citys) {
                            CityAreaDto cityAreaDto = new CityAreaDto();
                            Map map2 = new HashMap();
                            map2.put("area1", list.get(2));
                            map2.put("area2", list.get(1));
                            map2.put("area3", city);
                            List<String> regions = breedFactoryReadService.serachAllregion1(map2);
                            cityAreaDto.setCity(city);
                            cityAreaDto.setRegions(regions);
                            cityAreaDtoList.add(cityAreaDto);

                        }
                        map.put("cityAreaDtoList", cityAreaDtoList);
                        map.put("breedFactorys", breedFactoryList);
                        //switch break;
                        break;
                    case 2:
                        UserProfile userProfile2 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String city1 = userProfile2.getCity();
                        Map map7 = new HashMap();
                        map7.put("city", city1);
                        List<BreedFactory> breedFactoryList1 = (List<BreedFactory>) breedFactoryReadService.findByArea(map7).getResult();
                        //获取所有省下城市

                        Map map21 = new HashMap();
                        map21.put("area1", list.get(2));
                        map21.put("area2", list.get(1));
                        map21.put("area3", city1);
                        List<String> citys2 = breedFactoryReadService.serachAllregion1(map21);
                        //获取城市下所有区
                        List<CityAreaDto> cityAreaDtoList1 = new ArrayList<>();
                        for (String city : citys2) {
                            CityAreaDto cityAreaDto = new CityAreaDto();
                            Map map2 = new HashMap();
                            map2.put("area1", list.get(3));
                            map2.put("area2", list.get(2));
                            map2.put("area3", city);
                            List<String> regions = breedFactoryReadService.serachAllregion1(map2);
                            cityAreaDto.setCity(city);
                            cityAreaDto.setRegions(regions);
                            cityAreaDtoList1.add(cityAreaDto);

                        }
                        map.put("cityAreaDtoList", cityAreaDtoList1);
                        map.put("breedFactorys", breedFactoryList1);
                        //switch break;
                        break;
                    case 3://区
                        UserProfile userProfile3 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String region = userProfile3.getRegion();
                        Map map36 = new HashMap();
                        map36.put("region", region);
                        List<BreedFactory> breedFactoryList3 = (List<BreedFactory>) breedFactoryReadService.findByArea(map36).getResult();
                        //获取所有区下城市镇

                        Map map31 = new HashMap();
                        map31.put("area1", list.get(3));
                        map31.put("area2", list.get(2));
                        map31.put("area3", region);
                        List<String> citys3 = breedFactoryReadService.serachAllregion1(map31);
                        //获取对应镇 对应村
                        List<CityAreaDto> cityAreaDtoList3 = new ArrayList<>();
                        for (String city : citys3) {
                            CityAreaDto cityAreaDto = new CityAreaDto();
                            Map map2 = new HashMap();
                            map2.put("area1", list.get(4));
                            map2.put("area2", list.get(3));
                            map2.put("area3", city);
                            List<String> regions = breedFactoryReadService.serachAllregion1(map2);
                            cityAreaDto.setCity(city);
                            cityAreaDto.setRegions(regions);
                            cityAreaDtoList3.add(cityAreaDto);

                        }
                        map.put("cityAreaDtoList", cityAreaDtoList3);
                        map.put("breedFactorys", breedFactoryList3);
                        //switch break;
                        break;
                    case 4://镇
                        UserProfile userProfile4 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String street = userProfile4.getStreet();
                        Map map46 = new HashMap();
                        map46.put("street", street);
                        List<BreedFactory> breedFactoryList4 = (List<BreedFactory>) breedFactoryReadService.findByArea(map46).getResult();
                        //获取所有镇下所有村

                        Map map41 = new HashMap();
                        map41.put("area1", list.get(4));
                        map41.put("area2", list.get(3));
                        map41.put("area3", street);
                        List<String> citys4 = breedFactoryReadService.serachAllregion1(map41);//所有村

                        List<CityAreaDto> cityAreaDtoList4 = new ArrayList<>();

                        CityAreaDto cityAreaDto = new CityAreaDto();

                        cityAreaDto.setCity(street);
                        cityAreaDto.setRegions(citys4);
                        cityAreaDtoList4.add(cityAreaDto);


                        map.put("cityAreaDtoList", cityAreaDtoList4);
                        map.put("breedFactorys", breedFactoryList4);
                        //switch break;
                        break;
                    case 5://村
                        List<GovernmentUserDuty> governmentUserDutyList = governmentUserDutyReadService.findGovernmentUserDutiesByUserId(userResponse.getResult().getId(), null).getResult();
                        List<Long> longlist = new ArrayList<>();
                        if (governmentUserDutyList.size()!=0){
                            for (GovernmentUserDuty governmentUserDuty : governmentUserDutyList) {
                                longlist.add(governmentUserDuty.getFactoryOrPointId());
                            }
                        }
                        List<String> villages = new ArrayList<>();
                        List<BreedFactory> breedFactoryList2 = breedFactoryReadService.findBreedFactoryDtoByIds(longlist).getResult();
                        for (BreedFactory breedFactory : breedFactoryList2) {
                            villages.add(breedFactory.getVillage());
                            ;
                        }

                        List<String> listWithoutDup = new ArrayList<String>(new HashSet<String>(villages));


                        List<CityAreaDto> cityAreaDtoList5 = new ArrayList<>();

                        CityAreaDto cityAreaDto1 = new CityAreaDto();
                        cityAreaDto1.setCity(" ");

                        // cityAreaDto1.setCity(village);
                        cityAreaDto1.setRegions(listWithoutDup);

                        cityAreaDtoList5.add(cityAreaDto1);


                        map.put("cityAreaDtoList", cityAreaDtoList5);
                        map.put("breedFactorys", breedFactoryList2);
                        //switch break;
                        break;
                }


                break;
            }

        }

        return map;
    }


    @ResponseBody
    @RequestMapping("declarmanageDelete")
    public Boolean declarmanageDelete(@RequestParam Long id, Integer quantity, Long breedId) {

        try {
            BreedReport breedReport = new BreedReport();
            breedReport.setId(id);
            breedReport.setDeleteFlag(-1);
            Boolean a = breedReportWriteService.update(breedReport);
            ProductionReport productionReport = productionReportReadService.findProductByRelateId(id);
            if (productionReport != null) {
                productionReport.setDeleteFlag(-1);
                productionReportWriteService.update(productionReport);

            }


            if (quantity > 0) {
                int sum = breedFactoryReadService.findById(breedId).getResult().getStockQuantity() + quantity;
                BreedFactory breedFactory = new BreedFactory();
                breedFactory.setId(breedId);
                breedFactory.setStockQuantity(sum);
                breedFactoryWriteService.update(breedFactory).getResult();
            }
            return a;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    @ResponseBody
    @RequestMapping("quarantineunDelete")
    public Boolean quarantineunDelete(@RequestParam Long id, Integer quantity, Long breedId) {

        try {
            Harmless harmless = new Harmless();
            harmless.setId(id);
            harmless.setDeleteFlag(-1);
            Boolean a = harmlessWriteService.update(harmless);
            if (quantity > 0) {
                int sum = breedFactoryReadService.findById(breedId).getResult().getStockQuantity() + quantity;
                BreedFactory breedFactory = new BreedFactory();
                breedFactory.setId(breedId);
                breedFactory.setStockQuantity(sum);
                breedFactoryWriteService.update(breedFactory).getResult();
            }
            return a;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    public List findFactoryIdByArea(String region, int breedType) {

        Map map = new HashMap();
        if (breedType != -1) {
            map.put("breedType", breedType);
        }
        List<String> list1 = new ArrayList<>();
        String[] list = region.split(",");

        for (int i = 0; i < list.length; i++) {
            list1.add(list[i]);
        }
        map.put("areas", list1);

        List<BreedFactory> breedFactoryList = (List<BreedFactory>) breedFactoryReadService.findByArea(map).getResult();

        return breedFactoryList;
    }


}
